﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sebarf.Client;
using Sebarf.Core;
using Sebarf.Core.Interfaces;
using Sebarf.Server;

#endregion

namespace Sebarf.Tests {
	/// <summary>
	/// Description of the class
	/// </summary>
	[TestClass]
	public class TestRequestMultipleClients {
		#region Public Properties

		// public Properties located here

		#endregion

		#region Public Methods

		[TestMethod]
		public void RunTest() {
			IServer server =
				WcfBasedServer.Create(
					delegate { return RequestHandlerEngine.Create(null, new IRequestHandler[] { new MultiplyRequestHandler() }); });
			server.Start();

			IClient client = WcfBasedClient.Create("Client 1", null);
			client.Connect();

			Assert.AreEqual(Convert.ToInt32(client.Process(new MultiplyRequest { Number1 = 50, Number2 = 80 })), 50 * 80 + 1);
			Assert.AreEqual(Convert.ToInt32(client.Process(new MultiplyRequest { Number1 = 50, Number2 = 80 })), 50 * 80 + 2);
			Assert.AreEqual(Convert.ToInt32(client.Process(new MultiplyRequest { Number1 = 50, Number2 = 80 })), 50 * 80 + 3);
			Assert.AreEqual(Convert.ToInt32(client.Process(new MultiplyRequest { Number1 = 50, Number2 = 80 })), 50 * 80 + 4);

			IClient client2 = WcfBasedClient.Create("Client 2", null);
			client2.Connect();

			Assert.AreEqual(Convert.ToInt32(client2.Process(new MultiplyRequest { Number1 = 50, Number2 = 80 })), 50 * 80 + 1);

			IClient client3 = WcfBasedClient.Create("Client 1", null);
			client3.Connect();

			Assert.AreEqual(Convert.ToInt32(client3.Process(new MultiplyRequest { Number1 = 50, Number2 = 80 })), 50 * 80 + 5);
			server.Stop();
		}

		#endregion

		#region Private Methods

		// private methods located here

		#endregion

		#region Private Fields

		// private fields located here

		#endregion

		#region Nested type: MultiplyRequest

		[Serializable]
		public class MultiplyRequest : IRequest {
			public double Number1 { get; set; }
			public double Number2 { get; set; }

			public string Id { get; set; }
		}

		#endregion

		#region Nested type: MultiplyRequestHandler

		public class MultiplyRequestHandler : IRequestHandler {
			public int Index { get; set; }

			#region IRequestHandler Members

			public bool CanHandleRequest(IRequest request) {
				return request is MultiplyRequest;
			}

			public object ProcessRequest(IRequest request, IServiceLocatorService serviceLocator) {
				var msg = request as MultiplyRequest;
				Index++;
				return msg.Number1 * msg.Number2 + Index;
			}

			#endregion
		}

		#endregion
	}
}